import { StateCreator } from 'zustand';

export type BearState = {
  bears: number;
  obj: { a: number }
};

export type BearActions = {
  addBear: (count?: number) => void;
  removeAllBears: () => void;
  setObj: (obj: { a: number }) => void;
};

export const createBearSlice: StateCreator<
  BearState & BearActions,
  [["zustand/immer", never]],
  [],
  BearState & BearActions
> = 
  (set) => ({
    bears: 0,
    obj: { a: 1 },
    setObj: obj => set({ obj }),
    addBear: (count = 1) => set((state) => {
      // 利用 immer 可直接修改 state
      state.bears += count;
    }),
    removeAllBears: () => set({ bears: 0 })
  })
